package com.zhang.family_doctor.controller;

import com.zhang.family_doctor.dto.RolePurviewDto;
import com.zhang.family_doctor.entity.R;
import com.zhang.family_doctor.entity.Role;
import com.zhang.family_doctor.entity.RolePurview;
import com.zhang.family_doctor.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 角色模块
 */
@Api(tags = "角色模块相关接口")
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("新增角色")
    @PostMapping("/save")
    public R save(@RequestBody Role role, HttpServletRequest request){

        log.info("新增角色：{}",role);
        return roleService.save(role,request);
    }

    @ApiOperation("修改角色")
    @PostMapping("/update")
    public R update(@RequestBody Role role,HttpServletRequest request){
        log.info("修改角色：{}",role);
        return roleService.update(role,request);
    }

    @ApiOperation("查看分页的角色")
    @GetMapping("/getAll")
    public R<List<Role>> getAll(Integer pageNum, Integer pageSize){
        log.info("查看分页的角色：");
        return roleService.getAll(pageNum,pageSize);
    }

    @ApiOperation("根据id删除角色")
    @DeleteMapping("/deleteById")
    public R<String> deleteById(Integer id){
        log.info("根据id删除角色：{}",id);
        return roleService.deleteById(id);
    }

    @ApiOperation("给角色新增权限")
    @PostMapping("/saveRolePurview")
    public R<String> saveRolePurview(@RequestBody RolePurviewDto rolePurviewDto){
        log.info("给角色新增权限：{}",rolePurviewDto);
        return roleService.saveRolePurview(rolePurviewDto);
    }

    @ApiOperation("修改角色权限")
    @PostMapping("/updateRolePurview")
    public R<String> updateRolePurview(@RequestBody RolePurviewDto rolePurviewDto){
        log.info("修改角色权限:{}",rolePurviewDto);
        return roleService.updateRolePurview(rolePurviewDto);
    }

}
